﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chess
{
    class Program
    {
        static void Main(string[] args)
        {
            ChessBoard board= new ChessBoard();
            short t;
            board.Print();
            do
            {
                Console.Write("\nNguoi choi 1:");
                t = board.Move(1);
                board.Print();
                if (t != 0)
                    break;
                Console.Write("\nNguoi choi 2:");
                t = board.Move(2);
                board.Print();
            }while(t==0);
            if (t == 1)
                Console.WriteLine("NGUOI CHOI THU 1 LA NGUOI CHIEN THANG!");
            else
                Console.Write("NGUOI CHOI THU 2 LA NGUOI CHIEN THANG!");
            
                
            
            Console.ReadKey();
        }
    }
    public class ChessBoard
    {

        public ChessBoard()
        {
            short i, j;
            // khoi tao quan co cho nguoi choi 1
            i = 1;
            for (j = 0; j < 8; j++)
                chess[i, j] = new Chessman(i, j, 1, 1);
            i = 0;
            for (j = 0; j < 5; j++)
                chess[i, j] = new Chessman(i, j, (short)(j + 2), 1);
            chess[0,5]= new Chessman(0,5,4,1);
            chess[0,6]= new Chessman(0,6,3,1);
            chess[0,7]= new Chessman(0,7,2,1);
            // khoi tao quan co cho nguoi choi 2
            i = 6;
            for (j = 0; j < 8; j++)
                chess[i, j] = new Chessman(i, j, 1, 2);
            i = 7;
            for (j = 0; j < 5; j++)
                chess[i, j] = new Chessman(i, j, (short)(j + 2), 2);
            chess[7, 5] = new Chessman(7, 5, 4, 2);
            chess[7, 6] = new Chessman(7, 6, 3, 2);
            chess[7, 7] = new Chessman(7, 7, 2, 2); 
            // nhung khoi tao con lai
            i = 2;
            while (i <= 5)
            {
                for (j = 0; j < 8; j++)
                    chess[i, j] = new Chessman(i, j);
                i++;
            }
        }
        // xuat man hinh
        public void Print()
        {
            Console.WriteLine("\n\n");
            short i, j;
            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                        if (chess[i, j].Type != 0)
                        {
                            Console.Write("[");
                            if (chess[i, j].Type == 1)
                                Console.Write("Chot");
                            else if (chess[i, j].Type == 2)
                                Console.Write(" Xe ");
                            else if (chess[i, j].Type == 3)
                                Console.Write("Ngua");
                            else if (chess[i, j].Type == 4)
                                Console.Write("Tinh");
                            else if (chess[i, j].Type == 5)
                                Console.Write("Hau ");
                            else if (chess[i, j].Type == 6)
                                Console.Write("Vua ");
                            Console.Write("-");
                            if (chess[i, j].Player == 1)
                                Console.Write("1]");
                            else Console.Write("2]");
                        }
                        else Console.Write("--------");
                        if (j < 7)
                            Console.Write ("*");

                }
                if(i<7)
                Console.Write("\n*\n*\n");
               

            
            }
        
        }
        public short Move(short t)
        {
            short i, j, p, q,k=0;
            do
            {
                Console.Write("\nNhap vi tri quan co can di:\nx= ");
                i = Convert.ToInt16(Console.ReadLine());
                Console.Write("y= ");
                j = Convert.ToInt16(Console.ReadLine());
            }while(!((i>=0 && i<=7) && (j>=0 && j<=7) && (chess[i,j].Player == t)&&(chess[i,j].Type!=0)));
            do
            {
                Console.Write("\nNhap vi tri quan co duoc chon can den:\nx= ");
                p = Convert.ToInt16(Console.ReadLine());
                Console.Write("y= ");
                q = Convert.ToInt16(Console.ReadLine());
            }while(!(((p>=0 && p<=7) && (q>=0 && q<=7)) && Check(i,j,p,q)==1) || (i==p && j==q));
            if (chess[p, q].Type == 6)
                k = chess[i, j].Player;
            if (chess[i, j].Player == 1 && p == 7 && chess[i, j].Type == 1)
                chess[p, q] = new Chessman(p, q, 5, 1);
            else if (chess[i, j].Player == 2 && p == 0 && chess[i, j].Type == 1)
                chess[p, q] = new Chessman(p, q, 5, 2);
            else
                chess[p, q] = new Chessman(p, q, chess[i, j].Type, chess[i, j].Player);
                chess[i, j] = new Chessman(i, j);
                return k;

            
            
        
        
        
        }
        public Chessman[,] Chess
        {
            get { return chess; }
            set { chess = value; }
        }
        public short Check(short i, short j, short p, short q)
        {
            short t=0,m,n;
            if (chess[i, j].Type == 2)
            {
                if ((i != p && j != q) || (i==p && j==q))
                    return t;
                if (chess[i, j].Player == chess[p, q].Player)
                    return t;
                if (i != p)
                    if (i < p)
                    {
                        for (m = (short)(i + 1); m < p; m++)
                            if (chess[m, j].Type != 0)
                                return t;
                    }
                    else
                        for (m = (short)(i - 1); m > p; m--)
                            if (chess[m, j].Type != 0)
                                return t;
                if(j!=q)
                    if (j < q)
                    {
                        for (m = (short)(j + 1); m < q; m++)
                            if (chess[i, m].Type != 0)
                                return t;
                    }
                    else
                        for (m = (short)(j - 1); m > q; m--)
                            if (chess[i, m].Type != 0)
                                return t;
                return (t = 1);
            }
            if (chess[i, j].Type == 3)
            {
                if (chess[i, j].Player == chess[p, q].Player)
                    return 0;
                if ((i == p + 2 || i == p - 2) && (j == q - 1 || j == q + 1))
                    return 1;
                if ((j == q + 2 || j == q - 2) && (i == p - 1 || i == p + 1))
                    return 1;
                return 0;            
            }
            if (chess[i, j].Type == 4)
            {
                if (chess[i, j].Player == chess[p, q].Player)
                    return 0;
                if ((i - p == j - q) || (i - p == q - j) || (p - i == j - q) || (p - i == q - j))
                {
                    if (i > p)
                        if (j > q)
                            for (m = (short)(i - 1), n = (short)(j - 1); m > p; m--)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n--;
                            }
                        else
                            for (m = (short)(i - 1), n = (short)(j + 1); m > p; m--)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n++;
                            }
                    else
                        if (j > q)
                            for (m = (short)(i + 1), n = (short)(j - 1); m < p; m++)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n--;
                            }
                        else
                            for (m = (short)(i + 1), n = (short)(j + 1); m < p; m++)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n++;
                            }
                    return 1;
                }
                
                return 0;
                
            }
            if (chess[i, j].Type == 5)
            {
                if (chess[i, j].Player == chess[p, q].Player)
                    return 0;
                if ((i - p == j - q) || (i - p == q - j) || (p - i == j - q) || (p - i == q - j))
                {
                    if (i > p)
                        if (j > q)
                            for (m = (short)(i - 1), n = (short)(j - 1); m > p; m--)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n--;
                            }
                        else
                            for (m = (short)(i - 1), n = (short)(j + 1); m > p; m--)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n++;
                            }
                    else
                        if (j > q)
                            for (m = (short)(i + 1), n = (short)(j - 1); m < p; m++)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n--;
                            }
                        else
                            for (m = (short)(i + 1), n = (short)(j + 1); m < p; m++)
                            {
                                if (chess[m, n].Type != 0)
                                    return 0;
                                n++;
                            }
                    return 1;
                }
                if (!(i != p && j != q))
                {
                    if (i != p)
                        if (i < p)
                        {
                            for (m = (short)(i + 1); m < p; m++)
                                if (chess[m, j].Type != 0)
                                    return 0;
                        }
                        else
                            for (m = (short)(i - 1); m > p; m--)
                                if (chess[m, j].Type != 0)
                                    return 0;
                    if (j != q)
                        if (j < q)
                        {
                            for (m = (short)(j + 1); m < q; m++)
                                if (chess[i, m].Type != 0)
                                    return 0;
                        }
                        else
                            for (m = (short)(j - 1); m > q; m--)
                                if (chess[i, m].Type != 0)
                                    return 0;
                    return 1;
                
                }
            
            
            }
            if (chess[i, j].Type == 6)
            {
                if (chess[i, j].Player == chess[p, q].Player)
                    return 0;
                if ((i - p == j - q) || (i - p == q - j) || (p - i == j - q) || (p - i == q - j))
                    if (i - p == 1 || p - i == 1)
                        return 1;
                if (!(i != p && j != q))
                    if (i - p == 1 || p - i == 1 || j - q == 1 || q - j == 1)
                        return 1;
   
            }
            if (chess[i,j].Type == 1)
            {
                if (chess[i, j].Player == 1)
                {
                    if (p - i == 1 && q == j)
                        if (chess[p, q].Type == 0)
                            return 1;
                    if (p - i == 1 && (q - j == 1 || j - q == 1))
                        if (chess[p, q].Player == 2)
                            return 1;
                    if (p - i == 2 && q == j)
                        if (i == 1)
                            if (chess[p, q].Type == 0)
                                return 1;
                    return 0;
                
                }
                else if (chess[i, j].Player == 2)
                {
                    if (i - p == 1 && q == j)
                        if (chess[p, q].Type == 0)
                            return 1;
                    if (i - p == 1 && (q - j == 1 || j - q == 1))
                        if (chess[p, q].Player == 2)
                            return 1;
                    if (i - p == 2 && q == j)
                        if (i == 6)
                            if (chess[p, q].Type == 0)
                                return 1;
                    return 0;
                
                
                }
            
            }
            return 0;
        }
        Chessman[,] chess = new Chessman[8, 8];
    }
    public class Chessman
    {
        public Chessman(short a, short b)
        {
            this.x = a;
            this.y = b;
            this.type = 0;
            this.player = 0;
        }
        public Chessman(short a, short b, short p, short q)
        {
            this.x = a;
            this.y = b;
            this.type = p;
            this.player = q;
        }

        public short X
        {
            get { return x; }
            set { x = value; }
        }
        public short Y
        {
            get { return y; }
            set { y = value; }
        }
        public short Type
        {
            get { return type; }
            set { type = value; }
        }
        public short Player
        {
            get { return player; }
            set { player = value; }
        }
        short x;
        short y;
        short type;
        short player;
    
    
    }
}
